/**
* @Title: warning_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @date 2014年10月27日 下午4:43:38
* @version V1.0
*/
;
(function(){
         'use strict';
         
         var app = angular.module('fwa.mainApp');
         app.controller('CaseAuditDispathCtrl', ['$scope','$http','$modal','CaseCommonService','WarningAddService','BaseURL','Modal','FwaTabsetService','$stateParams','$state','WarningScreenService',
                                         function($scope,$http,$modal,CaseCommonService,WarningAddService,baseUrl,Modal,FwaTabsetService,$stateParams,$state,WarningScreenService){
       //事件对象
       $scope.problemObj={};
       
       //初始化请求参数
       var params = function (){
                var problemNo = "";
                if($stateParams.problemNo && !angular.isUndefined($stateParams.problemNo) ){
                         problemNo = $stateParams.problemNo;
                         $scope.problemNo=$stateParams.problemNo;
                }
                var params = {
                                   problemNo:problemNo
                }
                return params;
       }
                   
       /**
       * 初始化医疗机构数据 和医护人员信息和参保人信息
       */
        $scope.medicalCode='';
       function initMedicalData(){
                var parameter=params();
                parameter.taskType='1';//事件
                CaseCommonService.loadMedicalInfoList(parameter).success(function(response){
                         $scope.medicalData2=response.medicalBean.medicalList
                if($scope.medicalData2.length>0)
                {
                    if($scope.medicalData2[0].medicalCode)
                    {
                        $scope.medicalCode=$scope.medicalData2[0].medicalCode;
                    }
                }
                         $scope.staffData2=response.staffBean.staffList;
                         $scope.insuranceData2=response.insuranceBean.insuranceList;
                         
                         
                         CaseCommonService.initStaffAndInsuranceTempList($scope);
         				//更新当前页面数据
              		   WarningScreenService.findPointCutTempScose(
              				   WarningAddService.warningMarks,$stateParams.problemNo,'02',
              				   $scope.medicalData2[0],$scope.staffData2,$scope.insuranceData2);
                         
                         
                });
               
               
        }
                   
        /**
         * 初始化事件数据对象
         */
        $scope.object={};
                   function initProblemData(){
                            var parameter=params();
                            CaseCommonService.loadProblemData(parameter).success(function(response){
                                 var obj=response.problemObj;
                                 $scope.object=obj;         
                                     });
                   }
                            
                   initProblemData();
                   initMedicalData();

         $scope.warnMarkTempDetails=WarningAddService.warningMarks;
	       /**
	       * 分配事件
	       */
         $scope.dispatchOnClick=function(){
                var scope = $scope.$new();
                var modalInstance = $modal.open({
                      templateUrl: baseUrl+'views/hcpms/audit/problemManagement/caseDispatchSelectUserDialog_view.html',
                      backdrop:'static',
                           size:'sm',
                           scope:scope,
                           controller: 'selectDispatchUserModalCtrl'
                    });
         }
        /**
         * 医疗机构扣分事件
         */
        $scope.$on('fwaInvolve.medicalDeduction',function(e,v){
            $scope.marking (1,v, v.medicalCode, v.orgType);
        });
        /**
         * 医务人员扣分事件
         */
        $scope.$on('fwaInvolve.staffDeduction',function(e,v){
            $scope.marking(2,v,$scope.medicalCode,'');
        });
        //扣分 mark：1.机构扣分，2.医务人员扣分，3参保人员扣分,data:数据集合
        $scope.marking = function(mark,data,medicalCode,orgType){
            var medicalCodeTemp=null;
            if(medicalCode&&!angular.isUndefined(medicalCode)){
                medicalCodeTemp = medicalCode;
            }
            if(mark != 1 && medicalCodeTemp == null){
                Modal.alert("请先增加医疗机构信息！");
                return null;
            }else{
                medicalCode = medicalCodeTemp;
            }
            markData(mark,data,medicalCode,orgType);//扣分数据参数加载
            var modalInstance = $modal.open({
                templateUrl: baseUrl+'views/hcpms/warning/warnEdit/warningDeductionDialog_view.html',
                backdrop:'static',
                size:'sm',
                scope: $scope,
                //scope:scope,
                controller: 'WarningDeductionDialogCtrl'
            });
            modalInstance.result.then(function(item) {
                if (!WarningAddService.warningMarks) {
                    WarningAddService.warningMarks = [];
                }
                WarningAddService.warningMarks = item;
                //更新当前页面数据
    		   	WarningScreenService.findPointCutTempScose(
    		   		item,$stateParams.problemNo,'02',
    		   	$scope.medicalData2[0],$scope.staffData2,$scope.insuranceData2);
            });
        }
        //扣分数据参数加载
        var markData = function(mark,data,medicalCode,orgType){
            WarningAddService.mark = mark;
            WarningAddService.warningMarkData = {
                orgType:orgType,//0.机构，1.药店
                medicalCode:medicalCode,//medicalCode:所属机构编号
                code:data.selfCode,//code:编号
                name:data.name,//name:名称
                caseType:'02',//案件类型caseType：01.预警,02.稽核
                caseNo: $stateParams.problemCode,//事件编号
                happenDate: $scope.object.hearDate,//受理日期
                punishDateStart: $scope.object.problemStart,// 事件开始日期
                punishDateEnd: $scope.object.problemEnd,// 事件结束日期
                warningMarks:WarningAddService.warningMarks
            };
        }
        /**
	      * 返回事件
	      */
	    $scope.dispatchBackClick=function(){
		   	WarningAddService.warningMarks = WarningAddService.warningMarkTemps;//初始化扣分参数
            $state.go('problemManagement.problemManagementList');
        }
        /**
         * 查看事件信息对象中案件来源对话框
         */
	       $scope.$on('fwaAuditLeft.showModal',function(){
	                $scope.problemObj=$scope.object;
	                CaseCommonService.showProblemFromSourceDialog($scope);
	       });
	       
	       
	       
	       /**
	        * 调新增医务人员的回调函数
	        *  parameter={
	         * taskCode:'',
	         * taskType:'',1事件,2案件
	         * targetType:'',对象类型(1-医疗机构2-医生3-参保人)
	         * targetCode:''
	         * }
	        */
	       $scope.callStaffFun=function(array){
	    	   CaseCommonService.addTempStaffList(array,$scope);
	    	   
	       }
	       
	       /**
	        * 调新增参保人员的回调函数
	        */
	       $scope.callInsuranceFun=function(array){
	    	   CaseCommonService.addTempInsuranceListInfo(array,$scope);
	       }
	       
	       
	       /**
	        * 新增医护人员事件
	        */
	       $scope.$on('fwaInvolve.staffAdd',function(){
	    	   CaseCommonService.addStaffFun($scope);
	    	   
	       });
	       
	       
	       /**
	        * 新增参保人员事件
	        */
	       $scope.$on('fwaInvolve.insuranceAdd',function(){
	    	   CaseCommonService.addInsuranceFun($scope);
	    	   
	       });
	       
	        /**
	         * 点击医疗机构量事件
	         * 
	         */
	        $scope.$on('fwaInvolve.medicalCountClick',function(){
	     	   CaseCommonService.viewMedicalDetail($scope);
	        });
	       
	       /**
	        * 点击医务人员总数量事件
	        */
	       $scope.$on('fwaInvolve.staffCountClick',function(){
	       	CaseCommonService.viewStaffList($scope);
	       });
	       
	       /**
	        * 点击参保人总数量事件
	        */
	       $scope.$on('fwaInvolve.insuranceCountClick',function(){
	       	CaseCommonService.viewInsurList($scope);
	       });
	       
	       /**
	        * 医务人员删除
	        */
	       $scope.$on('fwaInvolve.staffDel',function(e,data){
	    	   Modal.confirm('确认删除医务人员'+data.name+'吗？',function(){
					WarningScreenService.deleteMark(WarningAddService.warningMarks,2,data);//mark:1.机构，2.医务人员，3.参保人
					 CaseCommonService.deleteTempStaffList(data,$scope);
				});
	    	    
	         });
	       
	       /**
	        * 参保人员删除
	        */
	       $scope.$on('fwaInvolve.insuranceDel',function(e,data){
	    	   Modal.confirm('确认删除参保人员'+data.name+'吗？',function(){
					WarningScreenService.deleteMark(WarningAddService.warningMarks,3,data);//mark:1.机构，2.医务人员，3.参保人
					 CaseCommonService.deleteTempInsuranceInfoList(data,$scope);
				});
	    	    
	         });
	       
	       
	       $scope.saveStaffAndInsuarnceInfo=function(){
	    	   saveStaffListInfo();
	    	   saveInsuranceInfo();
	       }
	       
	       
		   /**
		    * 暂存的时候对医务人员进行的保存
		    */
	       function saveStaffListInfo(){
		     CaseCommonService.saveTempStaffListInfo($scope,'1','2',$stateParams.problemNo);
		   
	       }
	       
	       /**
		    * 暂存的时候对参保人员进行的保存
		    */
	       function saveInsuranceInfo(){
	    	   CaseCommonService.saveTempInsuranceListInfo($scope,'1','3',$stateParams.problemNo);
	       }
	       

    }]);
})();
